﻿using System;
using System.Collections.Generic;
using System.Linq;
using LudeonTK;
using RimWorld;
using Verse;

namespace ARMIR {
    public class RosmontisGenerator {
        public static Pawn GenerateRosmontis(Faction faction,bool isAlone) {
            //创建pawn请求
            PawnGenerationRequest request = new PawnGenerationRequest(
                kind: PawnKindDefOf.Colonist, // 使用殖民者类型
                faction: faction, // 设置阵营
                context: PawnGenerationContext.NonPlayer, // 生成上下文
                fixedGender: Gender.Female, // 固定性别为女性
                fixedBirthName: "Rosmontis", // 固定名字
                biologicalAgeRange: new FloatRange(14,18)
            );

            // 生成pawn
            Pawn pawn = PawnGenerator.GeneratePawn(request);

            // 设置名字
            pawn.Name = new NameTriple(
                first: " ",
                nick: "Rosmontis",
                last: " "
            );

            //智人种迷迭香
            if (ModsConfig.BiotechActive) {
                pawn.genes.SetXenotype(XenotypeDefOf.Baseliner);
            }

            //穿戴
            pawn.apparel?.DestroyAll(); //清理原有衣物
            //穿戴衣物
            ThingDef shirtDef = DefDatabase<ThingDef>.GetNamed("ARMIR_Rosmontis_Shirt");
            if (shirtDef != null) {
                Thing shirt = ThingMaker.MakeThing(shirtDef);
                pawn.apparel.Wear((Apparel)shirt); // 强制穿戴
            }

            ThingDef CoatDef = DefDatabase<ThingDef>.GetNamed("ARMIR_Rosmontis_Coat"); // 你的服装Def
            if (CoatDef != null) {
                Thing Coat = ThingMaker.MakeThing(CoatDef);
                pawn.apparel.Wear((Apparel)Coat); // 强制穿戴
            }

            if (isAlone == false) {
                ThingDef TacticsMaterielDef = DefDatabase<ThingDef>.GetNamed("ARMIR_Tactics_Annihilated_Materiel"); // 你的服装Def
                if (TacticsMaterielDef != null)
                {
                    Thing TacticsMateriel = ThingMaker.MakeThing(TacticsMaterielDef);
                    pawn.apparel.Wear((Apparel)TacticsMateriel); // 强制穿戴
                }

                ThingDef FPAMindMixer = DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer"); // 你的服装Def
                if (FPAMindMixer != null)
                {
                    Thing FPAMM = ThingMaker.MakeThing(FPAMindMixer);
                    pawn.apparel.Wear((Apparel)FPAMM); // 强制穿戴
                }
            }
            //护盾
            ThingDef ShieldDef = DefDatabase<ThingDef>.GetNamed("ARMIR_mindShield");
            if (ShieldDef != null)
            {
                Apparel listShield = ThingMaker.MakeThing(ShieldDef) as Apparel;
                pawn.apparel.Wear(listShield, dropReplacedApparel: false, locked: true);// 强制穿戴不可取下
            }



            // 设置特性
            pawn.story.traits.allTraits.Clear();
            Trait lostMemory = new Trait(DefDatabase<TraitDef>.GetNamed("ARMIR_lostMemory"));
            pawn.story.traits.GainTrait(lostMemory);
            Trait ARMIR_loveFamily = new Trait(DefDatabase<TraitDef>.GetNamed("ARMIR_loveFamily"));
            pawn.story.traits.GainTrait(ARMIR_loveFamily);
            Trait ARMIR_psychicGrip = new Trait(DefDatabase<TraitDef>.GetNamed("ARMIR_psychicGrip"));
            pawn.story.traits.GainTrait(ARMIR_psychicGrip);

            // 设置技能
            foreach (SkillRecord skill in pawn.skills.skills)
            {
                if (skill.def == SkillDefOf.Melee) {
                    skill.passion = Passion.Minor;
                    skill.Level = 7;
                }
                if (skill.def == SkillDefOf.Shooting)
                {
                    skill.passion = Passion.Major;
                    skill.Level = 18;
                }
                if (skill.def == SkillDefOf.Intellectual) { 
                    skill.passion = Passion.Major;
                    skill.Level = 16;
                }
            }

            pawn.story.Childhood = DefDatabase<BackstoryDef>.GetNamed("ARMIR_kidRosmontis");
            pawn.story.Adulthood = DefDatabase<BackstoryDef>.GetNamed("ARMIR_adultRosmontis");

            //生物科技
            if (ModsConfig.BiotechActive) { 
                pawn.genes.ClearXenogenes();
                pawn.genes.AddGene(DefDatabase<GeneDef>.GetNamed("Rosmontis_tail"),true);
            }


            // 自定义外观
            pawn.story.hairDef = DefDatabase<HairDef>.GetNamed("ARMIR_Hair"); //发型
            pawn.story.HairColor = new UnityEngine.Color(1, 1, 1); // 设定为白毛捏
            pawn.story.headType = DefDatabase<HeadTypeDef>.GetNamed("ARMIR_Rosmontis_Head"); //头型
            pawn.story.bodyType = BodyTypeDefOf.Thin; //体型
            pawn.story.skinColorOverride = new UnityEngine.Color(1f, 1f, 1f);//肤色

            // 添加装备
            pawn.equipment.DestroyAllEquipment();
            Thing weapon = ThingMaker.MakeThing(ThingDef.Named("ARMIR_Rosmontis_NoteBook"));
            pawn.equipment.AddEquipment(weapon as ThingWithComps);

            // 设置hediff
            pawn.health.RemoveAllHediffs();
            BodyPartRecord brainPart = pawn.health.hediffSet.GetNotMissingParts().FirstOrDefault((BodyPartRecord x) => x.def == DefDatabase<BodyPartDef>.GetNamed("Brain"));
            if (brainPart != null)
            {
                Hediff implant = HediffMaker.MakeHediff(DefDatabase<HediffDef>.GetNamed("ARMIR_OriginiumBrainStem"), pawn, brainPart);
                pawn.health.AddHediff(implant);
            }

            return pawn;
        }

        // 调用示例（可以在命令或事件中调用）
        [DebugAction("Rosmontis In Rimworld", "Spawn Rosmontis", actionType = DebugActionType.Action)]
        public static void SpawnRosmontis() {
            Pawn ros = GenerateRosmontis(Faction.OfPlayer,false);
            GenSpawn.Spawn(ros, UI.MouseCell(), Find.CurrentMap);
            Messages.Message("Rosmontis has arrived!", MessageTypeDefOf.PositiveEvent);
        }
    }
}